﻿<%@ WebHandler Language="C#" Class="RBContainerAdvanceStartHandler" %>
using System;
using System.Web;
using BusinessRule.DTO;
using System.Web.SessionState;
using DatabaseAccess.DBUtility;
using System.Data.OracleClient;
using System.Data;
using Newtonsoft.Json;
using System.Collections.Generic;
using BusinessRule.Business;
using BusinessRule.IBusiness;
using BusinessRule.BusinessFactory;

/*Description: 热表提前开卡处理程序
'Copyright (c) : 通力凯顿（北京）系统集成有限公司
'Writer: Zhangrj 
'create Date:20220616
'Rewriter:
'Rewrite Date:
*/
public class RBContainerAdvanceStartHandler : IHttpHandler, IRequiresSessionState
{
    CurrentUserInfo m_UserInfo;
    RbContainerBusiness rbBusiness = new RbContainerBusiness();
    uLMSCommonBusiness commonBusiness = new uLMSCommonBusiness();
    uLMSContainerShipIBusiness m_ContainerShip = uMESBusinessFactory.instance.GetuLMSContainerShipObject();
    public void ProcessRequest(HttpContext context)
    {
        try
        {
            var action = context.Request["action"].ToString();
            m_UserInfo = (CurrentUserInfo)context.Session["CurrentUserInfo"];
            if (m_UserInfo == null)
            {
                context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Result = false, Message = "登录过期，请重新登录" }));
                return;
            }
            switch (action)
            {
                case "GetContainerList":
                    GetContainerList(context);
                    break;
                case "GetContainerDetail":
                    GetContainerDetail(context);
                    break;
                case "GetContainerStepDetail":
                    GetContainerStepDetail(context);
                    break;
                case "SetStartSession":
                    SetStartSession(context);
                    break;
                default:
                    break;
            }
        }
        catch (Exception ex)
        {
            var result = new uMESProcessResult();
            result.Result = false;
            result.Message = ex.Message;
            context.Response.ContentType = "text/html";
            context.Response.Write(JsonConvert.SerializeObject(result));
        }
    }
    /// <summary>
    /// 获取批次主列表
    /// </summary>
    /// <param name="context"></param>
    public void GetContainerList(HttpContext context)
    {
        Dictionary<string, string> para = new Dictionary<string, string>();
        para.Add("FactoryID", m_UserInfo.FactoryID);
        para.Add("CurrentPageIndex", context.Request["pageIndex"].ToString());
        para.Add("PageSize", context.Request["pageSize"].ToString());
        para.Add("ScanContainerName", context.Request["scanContainer"].ToString());
        if (!string.IsNullOrWhiteSpace(para["ScanContainerName"].ToString()))
        {
            para["ScanContainerName"] = commonBusiness.ContainerScan(para["ScanContainerName"].ToString());
        }
        para.Add("ScanOrderName", context.Request["scanOrder"].ToString());
        para.Add("ProductName", context.Request["productName"].ToString());
        para.Add("ContainerName", context.Request["containerName"].ToString());
        para.Add("StepName", context.Request["stepName"].ToString());
        para.Add("StepPlannedStartDate", context.Request["stepPlannedStartDate"].ToString());
        para.Add("StepPlannedEndDate", context.Request["stepPlannedEndDate"].ToString());
        uMESProcessResult result = new uMESProcessResult() { Result = true };
        result = rbBusiness.GetRBContainerAdvanceStartList(para);
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 获取批次详细列表
    /// </summary>
    /// <param name="context"></param>
    public void GetContainerDetail(HttpContext context)
    {
        Dictionary<string, string> para = new Dictionary<string, string>();
        para.Add("FactoryID", m_UserInfo.FactoryID);
        para.Add("MfgOrderName", context.Request["mfgOrderName"].ToString());
        para.Add("ProductId", context.Request["productId"].ToString());
        para.Add("WorkflowId", context.Request["workflowId"].ToString());
        para.Add("WorkflowstepId", context.Request["workflowstepId"].ToString());
        para.Add("TogetherFlag", context.Request["togetherFlag"].ToString());
        para.Add("ContainerNo", context.Request["containerNo"].ToString());
        uMESProcessResult result = new uMESProcessResult();
        DataTable dtContainer = rbBusiness.GetRBContainerAdvanceStartDetail(para);
        result.DBTable = dtContainer;
        result.Result = true;
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 获取批次工序详细列表
    /// </summary>
    /// <param name="context"></param>
    public void GetContainerStepDetail(HttpContext context)
    {
        DataTable dtContainer = JsonConvert.DeserializeObject<DataTable>(context.Request["params"].ToString());
        if (dtContainer.Rows.Count == 0)
        {
            context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Result = false, Message = "登录过期，请重新登录" }));
            return;
        }
        string strContainerIds = "";
        foreach (DataRow item in dtContainer.Rows)
        {
            strContainerIds += "'" + item["ContainerID"].ToString() + "',";
        }
        strContainerIds = strContainerIds.Trim(',');
        Dictionary<string, string> para = new Dictionary<string, string>();
        para.Add("FactoryID", m_UserInfo.FactoryID);
        para.Add("ContainerIDs", strContainerIds);
        uMESProcessResult result = new uMESProcessResult();
        DataTable dtStep = rbBusiness.GetRBContainerAdvanceStartStepDetail(para);
        result.DBTable = dtStep;
        result.Result = true;
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 设置开卡缓存
    /// </summary>
    /// <param name="context"></param>
    public void SetStartSession(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult() { Result=true};
        DataTable dtContainer = JsonConvert.DeserializeObject<DataTable>(context.Request["params"].ToString());
        if (dtContainer.Rows.Count == 0)
        {
            result.Result = false; result.Message = "登录过期，请重新登录";
            context.Response.Write(JsonConvert.SerializeObject(result));
            return;
        }
        
        DataTable dtNew = dtContainer.Clone();
        foreach (DataRow item in dtContainer.Rows)
        {
            //if (rbBusiness.IsExistsSubContainer(item["ContainerID"].ToString(),item["ShipStepName"].ToString()))//验证是否存在子卡
            //{
            //    result.Result = false; result.Message = string.Format("批次：{0}的工序{1}已存在子卡，请勿重复开卡", item["ContainerName"].ToString(), item["ShipStepName"].ToString());
            //    break;
            //}
            if (dtNew.Select("ContainerID='" + item["ContainerID"].ToString() + "'").Length == 0)
            {
                dtNew.Rows.Add(item.ItemArray);
                dtNew.AcceptChanges();
            }
        }
        //if (!result.Result)
        //{
        //    context.Response.Write(JsonConvert.SerializeObject(result));
        //    return;
        //}
        context.Session["PopupData"] = dtNew;//缓存开卡批次
        context.Session["PopupStepData"] = dtContainer;//缓存开卡批次工序
        context.Response.Write(JsonConvert.SerializeObject(result));
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}